// ActionScript file

package zakladki.AS3 {

import AS3.constants.combo.dataprovider.HouseHeatingDataProvider;
import AS3.constants.combo.dataprovider.HouseInsulationDataProvider;
import AS3.constants.combo.dataprovider.HouseMaterialDataProvider;
import AS3.constants.combo.dataprovider.HouseTypeDataProvider;
import AS3.constants.combo.dataprovider.HouseVentilationDataProvider;
import AS3.constants.combo.dataprovider.HouseWinterTempDataProvider;

import mx.collections.*;
import mx.controls.*;
import mx.controls.Alert;
import mx.core.FlexGlobals;


public class HouseChangeListener {


    private var houseTypeCombo:ComboBox;
    private var materialTypeCombo:ComboBox;
    private var insulationCombo:ComboBox;
    private var heatingCombo:ComboBox;
    private var heatingSourceCombo:ComboBox;
    private var ventilationCombo:ComboBox;
    private var areaSelection:NumericStepper;
    private var personNumSelection:NumericStepper;
    private var renewableSelection:NumericStepper;

    private var mainApplication:Fossilminator = FlexGlobals.topLevelApplication as Fossilminator;

    public function initialize(houseType:ComboBox,materialType:ComboBox,insulation:ComboBox,heating:ComboBox,heatingSource:ComboBox,ventilation:ComboBox,area:NumericStepper,persons:NumericStepper,renewable:NumericStepper):void {
        houseTypeCombo = houseType;
        materialTypeCombo = materialType;
        insulationCombo = insulation;
        heatingCombo = heating;
        heatingSourceCombo = heatingSource;
        ventilationCombo = ventilation;
        areaSelection = area;
        personNumSelection = persons;
        renewableSelection = renewable;
    }

    public function houseTypeChanged(calc:Boolean):void{
        try {
            var selectedData:HouseSelectedData = createHouseSelectedData();

            mainApplication.widok_sceny.houseTypeChanged(selectedData.houseType, selectedData.buildingMaterial);
            mainApplication.widok_sceny.houseInsulationChanged(selectedData.houseType, selectedData.insulation);
            mainApplication.widok_sceny.houseTempChanged(selectedData.houseType, selectedData.heating);
            mainApplication.widok_sceny.houseHolderViewChanged(selectedData.personNumber, selectedData.houseType);
            mainApplication.widok_sceny.houseHeatingChanged(selectedData.heatingSource,selectedData.houseType);

            if (calc) {
                this.applySelectedData(selectedData);
            }
        } catch(e:Error) {
            Alert.show("Error change");
        }
    }

    public function materialTypeChanged(calc:Boolean):void {
        var selectedData:HouseSelectedData = createHouseSelectedData();
        mainApplication.widok_sceny.houseTypeChanged(selectedData.houseType,selectedData.buildingMaterial);
        if(calc){
            this.applySelectedData(selectedData);
        }
    }

    public function insulationChanged(calc:Boolean):void {
        var selectedData:HouseSelectedData = createHouseSelectedData();
        mainApplication.widok_sceny.houseInsulationChanged(selectedData.houseType,selectedData.insulation);
        if(calc){
            this.applySelectedData(selectedData);
        }
    }

    public function areaChanged(calc:Boolean):void {
        if(calc){
            this.applySelectedData(createHouseSelectedData());
        }
    }

    public function personNumberChanged(calc:Boolean):void {
        var selectedData:HouseSelectedData = createHouseSelectedData();
        mainApplication.widok_sceny.houseHolderViewChanged(selectedData.personNumber + 1,selectedData.houseType);
        if(calc){
            this.applySelectedData(selectedData);
        }
    }

    public function houseTempChanged(calc:Boolean):void {
        var selectedData:HouseSelectedData = createHouseSelectedData();
        mainApplication.widok_sceny.houseTempChanged(selectedData.houseType,selectedData.heating);
        if(calc){
            try {
                this.applySelectedData(selectedData);
            } catch(e:Error) {
                Alert.show("cboOgrzewanie change error " + e.message);
            }
        }
    }

    public function heatingSourceChanged(calc:Boolean):void {
        var selectedData:HouseSelectedData = createHouseSelectedData();
        mainApplication.widok_sceny.houseHeatingChanged(selectedData.heatingSource,selectedData.houseType);
        if(calc){
            applySelectedData(selectedData);
        }
    }

    public function ventilationMethodChanged(calc:Boolean):void {
        if(calc){
            var selectedData:HouseSelectedData = createHouseSelectedData();
            applySelectedData(selectedData);
        }
    }

    public function renewableRatioChanged(calc:Boolean):void{
        if(calc){
            applySelectedData(createHouseSelectedData());
        }
    }

    private function createHouseSelectedData():HouseSelectedData{

        var houseType:int = houseTypeCombo.selectedItem.data;
        var buildingMaterial:int = materialTypeCombo.selectedItem.data;
        var insulation:int = insulationCombo.selectedItem.data;
        var heating:int = heatingCombo.selectedItem.data;
        var heatingSource:int = heatingSourceCombo.selectedItem.data;
        var ventilationType:int = ventilationCombo.selectedItem.data;
        var area:int = areaSelection.value;
        var personNumber:int = personNumSelection.value;
        var renewableRatio:int = renewableSelection.value;

        return new HouseSelectedData(houseType,buildingMaterial,insulation,heating,heatingSource,ventilationType,area,personNumber,renewableRatio);
    }

    private function applySelectedData(selectedData:HouseSelectedData):void {
        mainApplication.updateHouse(selectedData);
        mainApplication.calculate();
    }


}
}


		
	


	
	
				
